package com.iretailer.web;

import com.iretailer.quartz.common.Task;
import com.iretailer.quartz.common.TaskService;
import com.iretailer.util.ResponeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
public class TaskController {

    @Autowired
    private TaskService taskService;

    @RequestMapping(value = "/allTask.action")
    public void findAllTask(HttpServletRequest request, HttpServletResponse response) {
        List<Task> allTask = taskService.findAll();
        ResponeUtil.sendSuccess(response, allTask);
    }

    /**
     * 暂停一个任务
     *
     * @param request
     * @param response
     * @param map
     */
    @RequestMapping(value = "/pauseJob.action")
    public void pauseOneJob(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> map) {
        String id = (String) map.get("id");
        boolean status = taskService.pauseJob(id);
        ResponeUtil.sendSuccess(response,status);
    }

    /**
     * 恢复一个任务
     *
     * @param response
     * @param map
     */
    @RequestMapping(value = "/resumeJob.action")
    public void resumeJob(HttpServletResponse response, @RequestBody Map<String, Object> map) {
        String id = (String) map.get("id");
        boolean status = taskService.resumeJob(id);
        ResponeUtil.sendSuccess(response, status);
    }

    /**
     * 删除一个任务
     *
     * @param response
     * @param map
     */

    @RequestMapping(value = "/deleteJob.action")
    public void deleteJob(HttpServletResponse response, @RequestBody Map<String, Object> map) {
        String id = (String) map.get("id");
        boolean status = taskService.deleteJob(id);
        ResponeUtil.sendSuccess(response, status);
    }

    /**
     * 立即执行
     *
     * @param response
     * @param map
     */
    @RequestMapping(value = "/testJob")
    public void testJob(HttpServletResponse response, @RequestBody Map<String, Object> map) {
        String id = (String) map.get("id");
        boolean status = taskService.testJob(id);
        ResponeUtil.sendSuccess(response, status);
    }
}
